package com.unytech.project.utils;

import java.util.ArrayList;
import java.util.List;

import com.unytech.project.common.Globals;
import com.unytech.project.entity.system.Permission;

public class StringUtil {

	public static String analyRUI(String uri) throws Exception {
		List<String> permname = new ArrayList<String>();
		List<Permission> permlist = PermissionUtil.getXMLPermission();
		Permission curperm = null;
		for (int i=0; i<permlist.size(); i++) {
			curperm = permlist.get(i);
			if (curperm.getPermuri().equals(uri)) {
				permname.add(curperm.getPermname());
				break;
			}
		}
		if (permname.size() !=0 ) {
			do {
				Permission permtemp = null;
				for (int i=0; i<permlist.size(); i++) {
					permtemp = permlist.get(i);
					if (permtemp.getPermuri().equals(curperm.getParentid())) {
						permname.add(permtemp.getPermname());
						curperm = permtemp;
						break;
					}
				}
				
			} while (curperm.getParentid() != Globals.ROOTPERMISSIONID);
			
			StringBuffer name = new StringBuffer();
			for (int i=permname.size()-1; i>=0; i--) {
				name.append(permname.get(i));
				if (i != 0)
					name.append("-->");
			}
			return name.toString();
		} else {
			return "无：系统调用";
		}
	}
	
	public static String analyRUI(List<Permission> permlist, String uri) throws Exception {
		List<String> permname = new ArrayList<String>();
		if (permlist == null)
		permlist = PermissionUtil.getXMLPermission();
		
		Permission curperm = null;
		for (int i=0; i<permlist.size(); i++) {
			curperm = permlist.get(i);
			if (curperm.getPermuri().equals(uri)) {
				permname.add(curperm.getPermname());
				break;
			}
		}
		if (permname.size() !=0 ) {
			do {
				Permission permtemp = null;
				for (int i=0; i<permlist.size(); i++) {
					permtemp = permlist.get(i);
					if (permtemp.getPermuri().equals(curperm.getParentid())) {
						permname.add(permtemp.getPermname());
						curperm = permtemp;
						break;
					}
				}
				
			} while (curperm.getParentid() != Globals.ROOTPERMISSIONID);
			
			StringBuffer name = new StringBuffer();
			for (int i=permname.size()-1; i>=0; i--) {
				name.append(permname.get(i));
				if (i != 0)
					name.append("-->");
			}
			return name.toString();
		} else {
			return "无：系统调用";
		}
	}
}
